################################################################################ 
#
# The distribution of hate speech and its implications for content moderation
# PSRM - Replication package
# Figure C5
#
################################################################################ 


library(readr)
library(ggplot2)
library(forcats)
library(patchwork)

rm(list = setdiff(ls(), ls(pattern = "^wd|^setsave$")))

# Load Data
ch <- read_rds(paste0(wd_data_processed, "/twitter_user_info_aggregated/hate_speech_quartiles_ch_data.rds"))
us <- read_rds(paste0(wd_data_processed, "/twitter_user_info_aggregated/hate_speech_quartiles_us_data.rds"))

# Function to update the point size in all GeomPoint layers of a ggplot object
update_point_size <- function(plot, new_size = 1) {
  for (i in seq_along(plot$layers)) {
    # Check if the layer is a point layer
    if (inherits(plot$layers[[i]]$geom, "GeomPoint")) {
      plot$layers[[i]]$aes_params$size <- new_size
    }
  }
  return(plot)
}

# Update the point size for both plots from 2 to 1
us <- update_point_size(us, new_size = 1.5)
ch <- update_point_size(ch, new_size = 1.5)

# Adjust the second plot (ch) to remove the y-axis text.
ch <- ch + theme(axis.text.y = element_blank())
us <- us + theme(legend.position = "right",
                 plot.margin = margin(5, 5, 5, 5),
                 plot.title = element_text(hjust = 0.5))
ch <- ch + theme(legend.position = "right",
                 plot.margin = margin(5, 5, 5, 5),
                 plot.title = element_text(hjust = 0.5))

us <- us + ggtitle("United States")
ch <- ch + ggtitle("Switzerland")

# Combined plot
combined_plot <- (us + ch + 
                    plot_layout(ncol = 2, guides = "collect")) & 
  theme(legend.position = "bottom")

ggsave(plot = combined_plot, filename = paste0(wd_res, "/figures/figC5.png"), device = "png", width = 9, height = 6, dpi = 300, bg = "white")
cat("\n====================\n")
cat("Saved Figure C5")
cat("\n====================\n")